# pip install zhipuai 请先在终端进行安装

from zhipuai import ZhipuAI

client = ZhipuAI()

response = client.chat.completions.create(
    model="glm-4-0520",
    messages=[
        {
            "role": "system",
            "content": "你是一个理财小助手，你的任务是为用户分析他的理财信息，并为其提供理财服务。" 
        },
        {
            "role": "user",
            "content": "联网帮我查看今天阿里巴巴美股股价"
        }
    ],
    top_p= 0.7,
    temperature= 0.95,
    max_tokens=1024,
    tools = [
        {"type":"web_search","web_search":{"search_result":True}},
    #     {"type":"web_search","web_search":{"search_result":True}},
    #          {
    #     "type": "function",
    #     "function": {
    #         "name": "visualize_stock_data",
    #         "description": "用于查看股票图",
    #         "parameters": {
    #             "type": "object",
    #             "properties": {
    #             },
    #             "required": []
    #         }
    #     }
    #   },
      ],
    stream=True,
)
for trunk in response:
    print(trunk)

"""
EIP
DDNS
服务商IP
"""

import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
def visualize_stock_data():
    data = [
        {
        "ticker": "600519",
        "date": "2024-01-05",
        "open": 1661.33,
        "high": 1678.66,
        "low": 1652.11,
        "close": 1663.36,
        "volume": 2024286
        },
        {
        "ticker": "600519",
        "date": "2024-01-04",
        "open": 1693,
        "high": 1693,
        "low": 1662.9301,
        "close": 1669,
        "volume": 2155107
        }
    ]   
    # 解析日期并排序数据
    data.sort(key=lambda x: datetime.strptime(x['date'], '%Y-%m-%d'))

    # 提取数据
    dates = [datetime.strptime(item['date'], '%Y-%m-%d') for item in data]
    opens = [item['open'] for item in data]
    highs = [item['high'] for item in data]
    lows = [item['low'] for item in data]
    closes = [item['close'] for item in data]

    # 创建图表
    fig, ax = plt.subplots()

    # 绘制开盘价、最高价、最低价和收盘价
    ax.plot(dates, opens, label='开盘价', marker='o')
    ax.plot(dates, highs, label='最高价', marker='^')
    ax.plot(dates, lows, label='最低价', marker='v')
    ax.plot(dates, closes, label='收盘价', marker='s')

    # 设置日期格式
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
    ax.xaxis.set_major_locator(mdates.DayLocator())
    plt.gcf().autofmt_xdate() # 自动旋转日期标记

    # 添加图例
    plt.legend()

    # 添加标题和标签
    plt.title('股票价格走势图')
    plt.xlabel('日期')
    plt.ylabel('价格')

    # 显示图表
    plt.show()

# 示例数据
